package com.yanqu.road.server.manager.log;

import com.yanqu.data.analytics.enums.eUserIdAttr;
import com.yanqu.data.analytics.helper.DataBuilder;
import com.yanqu.road.entity.log.maidian.BaseLogEntity;
import com.yanqu.road.entity.log.maidian.ILogTraceBuilder;
import com.yanqu.road.server.manager.CrossDataAnalyticsMgr;

import java.util.Map;

/**
 * @Description 区服运营日志上报数数平台(目前是单例，不要有成员属性)
 * @Author wmm
 * @Data 2023/11/30 18:25
 */
public class CrossServerLogTraceBuilder implements ILogTraceBuilder {

    @Override
    public void buildCommonData(BaseLogEntity entity) {
        try {
            if(!CrossDataAnalyticsMgr.openDataAnalytics()){
                return;
            }
            if(entity.getUserId() > 0){
                DataBuilder dataBuilder = DataBuilder.newBuilder(entity.getUserId(), "");
                entity.setCommonDataMap(dataBuilder.getDataMap());
            }
        }catch (Exception e){
            CrossDataAnalyticsMgr.getLogger().info("error:", e);
        }
    }

    @Override
    public void trace(String eventName, Map<String, Object> dataMap) {
        try {
            if(dataMap == null){
                return;
            }
            if(!CrossDataAnalyticsMgr.openDataAnalytics()){
                return;
            }
            CrossDataAnalyticsMgr.provider.track(
                    String.valueOf(dataMap.get(eUserIdAttr.ACCOUNT_ID.getAttrId())),
                    String.valueOf(dataMap.get(eUserIdAttr.DISTINCT_ID.getAttrId())),
                    eventName,
                    dataMap);
        }catch (Exception e){
            CrossDataAnalyticsMgr.getLogger().info("error:", e);
        }

    }
}
